package com.qinghe.plant.controller;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.qinghe.plant.util.CodeType;
import com.qinghe.plant.util.Response;
import com.qinghe.plant.dao.Abnorma;
import com.qinghe.plant.model.AbnormaReponsitory;

@RestController
public class AbnormaController {

	@Autowired
	private AbnormaReponsitory abnormaReponsitory;
	
	@RequestMapping(value = "doReadAbnormas", method = RequestMethod.POST)
	public Response ReadAbnormas(HttpServletRequest request) {
		List<Abnorma> abnormas=abnormaReponsitory.findAll();
		return new Response(CodeType.Success,JSONArray.toJSONString(abnormas),"操作成功");
	}
	
	@RequestMapping(value = "doReadAbnorma", method = RequestMethod.POST)
	public Response ReadAbnorma(HttpServletRequest request) {
		String id =request.getParameter("id");
		Abnorma abnorma = FindAbnroma(id);
		return new Response(CodeType.Success,JSONObject.toJSONString(abnorma),"操作成功");
	}

	@RequestMapping(value = "doDeleteAbnorma", method = RequestMethod.POST)
	public Response DeleteAbnorma(HttpServletRequest request) {
		String id =request.getParameter("id");
		abnormaReponsitory.deleteById(id);
		return new Response(CodeType.Success,null,"操作成功");
	}
	
	@RequestMapping(value="doAlterAbnorma",method=RequestMethod.POST)
	public Response AlterAbnorma(HttpServletRequest request)
	{
		Abnorma abnorma=JSONObject.parseObject(request.getParameter("abnorma"), Abnorma.class);
		abnorma= abnormaReponsitory.save(abnorma);
		return new Response(CodeType.Success,JSONArray.toJSONString(abnorma),"操作成功");		
	}
	
	@RequestMapping(value="doInsertAbnorma",method=RequestMethod.POST)
	public Response InsertAbnorma(HttpServletRequest request)
	{
		if(HasAbnorma(request.getParameter("id")))
		{
			return new Response(CodeType.Failed,null,"数据已经存在！");
		}
		else 
		{
			Abnorma abnorma=new Abnorma();
			abnorma.setEid(request.getParameter(""));
			abnorma.setFault(Integer.parseInt(request.getParameter("")));
			abnorma.setLevel(Integer.parseInt(request.getParameter("")));
			abnorma.setTimer(new Date());
			abnorma= abnormaReponsitory.save(abnorma);
			return new Response(CodeType.Success,JSONObject.toJSONString(abnorma),"操作成功");
		}
	}
		
	private Abnorma FindAbnroma(String id) {
		@SuppressWarnings("serial")
		List<Abnorma> result = abnormaReponsitory.findAll(new Specification<Abnorma>() {
			@Override
			public Predicate toPredicate(Root<Abnorma> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
				List<Predicate> list = new ArrayList<Predicate>();

				list.add(cb.equal(root.get("id").as(String.class), id));
				Predicate[] p = new Predicate[list.size()];
				return cb.and(list.toArray(p));
			}
		});

		return (result.size()!=0)?result.get(0):null;
	}	
	
	private boolean HasAbnorma(String id) {
		@SuppressWarnings("serial")
		List<Abnorma> result = abnormaReponsitory.findAll(new Specification<Abnorma>() {
			@Override
			public Predicate toPredicate(Root<Abnorma> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
				List<Predicate> list = new ArrayList<Predicate>();
				list.add(cb.equal(root.get("id").as(String.class), id));

				Predicate[] p = new Predicate[list.size()];
				return cb.and(list.toArray(p));
			}
		});
		return (result.size()!=0)?true:false;
	}	
}
